<!-- src/components/TeacherChangePasswordContent.vue -->
<template>
  <div class="change-password-container">
    <el-card class="change-password-card">
      <el-form :model="changePasswordForm" :rules="rules" ref="changePasswordForm" label-width="100px" class="change-password-form">
        <el-form-item label="原密码" prop="oldPassword">
          <el-input type="password" v-model="changePasswordForm.oldPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input type="password" v-model="changePasswordForm.newPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input type="password" v-model="changePasswordForm.confirmPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('changePasswordForm')">确认修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';
export default {
  name: 'ChangePasswordContent',
  data() {
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.changePasswordForm.newPassword) {
        callback(new Error('两次输入的密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      changePasswordForm: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入原密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认新密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 处理修改密码逻辑
          let userId = localStorage.getItem("userId") || ''
          let oldPassword = this.changePasswordForm.oldPassword
          let newPassword = this.changePasswordForm.newPassword

          axios.put("/apiuser/changePass",{
            userId,
            oldPassword,
            newPassword
          }).then(response => {
            this.$message.success("修改密码成功")
          }).catch(err=>{


            if(err.response && err.response.data){
              this.$message.error(err.response.data)
            }else{
              this.$message.error("原密码输入有误")
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.change-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 70vh;
}

.change-password-card {
  width: 400px;
  padding: 20px;
}

.change-password-form {
  margin-top: 20px;
}
</style>
